package com.wolf.cx330.utils;

import android.graphics.Bitmap;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class AppUtils {
    /**
     * 将Bitmap 存到本地
     *
     * @param path
     * @param bitmap
     */
    public static void saveBitmap(String path, Bitmap bitmap) {
        File file = new File(path);
        File fileParent = file.getParentFile();
        if (fileParent != null && !fileParent.exists()) {
            boolean s = fileParent.mkdirs();
        }
        if (file.exists()) {
            boolean s = file.delete();
        }

        try {
            boolean s = file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }

        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);

        bitmap.recycle();

        try {
            if (fos != null) {
                fos.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
